﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Newtonsoft.Json;
using Tools;
using JHStock.Update;
using JHStock;

namespace JHStock
{
	public class NetKData
	{
        public NetKData(JHConfig Jhconfig, string _kdatatype,bool bcustommode = false)
        {
            this.Jhconfig = Jhconfig;
            this._kdatatype = _kdatatype;          
            this._bcustom = bcustommode;
			this._NetObjTags = null;
            this.CustomDealStocks = new List<Stock>();
			DaysCount = 0;
            FinMode = false;
        }
		public void GetNetKData(){  //check and DownLoad
			try{
        		int dayscount = DaysCount;
        		if(dayscount < 0)
        			return;
                if (FinMode)
                    GetDataFromNetFin(dayscount);
                else
                    GetDataFromNet(dayscount);
            }
            finally{
                CompleteRun();
			}
		}
        private void GetDataFromNet(int dayscount)
        {
            ThreadUpdateStocksConfig TSC1 = new ThreadUpdateStocksConfig(_kdatatype, dayscount);
            FormatDataFunction FD = new FormatDataFunction(_kdatatype);
            TSC1.UrlT = new ThreadUpdateUrlTemplate(_kdatatype);
            TSC1.Files = new ThreadUpdateJsonFiles();
            TSC1.MaxThreadSum = 50;
            TSC1.FormatData = FD.FormatKDData;
            ThreadUpdateStocksJson TS = new ThreadUpdateStocksJson(stocks, TSC1);
            if (ThreadShowMsg != null)
               TS.showmsg = new ShowDeleGate(ThreadShowMsg);

            _NetObjTags = new NetObjectTagstocks(DateTime.Now, TS.Tag);

            List<Stock> Dealstocks;
            if (!_bcustom || CustomDealStocks.Count == 0)
                Dealstocks = new List<Stock>(stocks.stocks);
            else
                Dealstocks =new List<Stock>( CustomDealStocks );

            //Dealstocks = Dealstocks.Take(10).ToList();zduan
            // Add 上证指数
            TS.Tag[0].Init(Sse);
            Dealstocks.Add(Sse);
            TS.SetTagValidValue(Dealstocks);
            TS.UpdateItem(Dealstocks );
            List<Stock> stockslist = Dealstocks;
            int stockcountb = stockslist.Count;

            
            stockslist = TS.Tag.Where(r =>r!=null&& r.value < 0 && r.value > -10 ).Select(r => r.s).ToList();
            if (stockslist.Count > 0)
            {
                string txt = string.Join( "\r\n",stockslist.Select(r1 => r1.ID + "\t"+r1.Name + "\t"+r1.PYCode).ToList());
                MFile.AppendAllText(this.stocks.Pathconfig.NowWorkPath() + _kdatatype + "_中断需要重新下载.log", "被中断的股票名称\r\n");
                TS.UpdateItem(stockslist);
            }
        }
        private void GetDataFromNetFin(int dayscount)
        {
            string datetype = "dayly";
            ThreadUpdateStocksConfig TSC1 = new ThreadUpdateStocksConfig(datetype, dayscount);
            FormatDataFunction FD = new FormatDataFunction("fin");
            TSC1.UrlT = new ThreadUpdateUrlTemplate(datetype,true);
            TSC1.Files = new ThreadUpdateJsonFiles();
            TSC1.MaxThreadSum = 20;
            TSC1.FormatData = FD.FormatKDData;
            ThreadUpdateStocksJson TS = new ThreadUpdateStocksJson(stocks, TSC1);
            if (ThreadShowMsg != null)
                TS.showmsg = new ShowDeleGate(ThreadShowMsg);

            _NetObjTags = new NetObjectTagstocks(DateTime.Now, TS.Tag);
            List<Stock> Dealstocks;//= new List<Stock>(_stocks.stocks);
            if (!_bcustom || CustomDealStocks.Count == 0)
                Dealstocks = new List<Stock>(stocks.stocks);
            else
                Dealstocks = new List<Stock>(CustomDealStocks);

            TS.SetTagValidValue(Dealstocks);
            TS.UpdateItem(Dealstocks);
            List<Stock> stockslist = Dealstocks;
            int stockcountb = stockslist.Count;
            
            stockslist = TS.Tag.Where(r => r != null && r.value < 0 && r.value > -10).Select(r => r.s).ToList();
            if (stockslist.Count > 0)
            {
                TS.UpdateItem(stockslist);
            }
        }
        public void SetCustomMode(bool mode = true)
        {
            _bcustom = mode;
        }
        public List<Stock> CustomDealStocks { get; set; }
		public int DaysCount{ get; set; }
        public bool FinMode { get; set; }
       
		public string ErrorMsg;
		public NetObjectTagstocks _NetObjTags;
		public ShowDeleGate ThreadShowMsg;
        public CompleteDeleGate CompleteRun;
        //SSE 上证指数
        private Stock Sse{get {return stocks.Sse;}}
		private Stocks stocks{get {return Jhconfig.Stocks;}}
        private bool _bcustom;
        private string _kdatatype;
        private JHConfig Jhconfig;

    }   
}
